package com.cloudlead.dictionaryAction.service.impl;

import com.cloudlead.commonData.service.TbDictionaryService;
import com.cloudlead.commonData.vo.BaseDictionary;
import com.cloudlead.commonData.vo.TbDictionary;
import com.cloudlead.dictionaryAction.service.TbDictionaryEditSer;
import com.cloudlead.dictionaryService.vo.TbDictionaryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wyj on 2015/8/18.
 */
@Service
public class TbDictionaryEditSerImpl implements TbDictionaryEditSer {
    @Autowired
    TbDictionaryService tbDictionaryService;//spring 注入

    @Override
    //单值查询
    public TbDictionaryVo findOneDictionary(TbDictionaryVo vo) {
        TbDictionary td = new TbDictionary();
        BeanUtils.copyProperties(vo, td);//对象类型转换vo-td
        if(vo.getBaseDictionary()!=null){//拷贝时对象无法拷贝，单独拷贝
            BaseDictionary bd=new BaseDictionary();
            BeanUtils.copyProperties(vo.getBaseDictionary(),bd);//拷贝基础字典
            td.setBaseDictionary(bd);
        }
        td=tbDictionaryService.findOne(td);//单值查询
        TbDictionaryVo vo1 = new TbDictionaryVo();
        BeanUtils.copyProperties(td, vo1);//转换对象类型为TbDictionaryVo
        return vo1 ;
    }

    @Override
    //多值查询
    public List<TbDictionaryVo> findAllDictionary(TbDictionaryVo vo) {
        TbDictionary td = new TbDictionary();
        BeanUtils.copyProperties(vo, td);//对象类型转换vo-td
        if(vo.getBaseDictionary()!=null){//拷贝时对象无法拷贝，单独拷贝
            BaseDictionary bd=new BaseDictionary();
            BeanUtils.copyProperties(vo.getBaseDictionary(),bd);//拷贝基础字典
            td.setBaseDictionary(bd);
        }
        List<TbDictionary> lv = (List<TbDictionary>)tbDictionaryService.list(td,null);//列表查询
        List<TbDictionaryVo> lvo = new ArrayList<TbDictionaryVo>();
       for(Iterator<TbDictionary> it = lv.iterator();it.hasNext();) {//遍历
           TbDictionaryVo tdv = new TbDictionaryVo();
           BeanUtils.copyProperties(it.next(), tdv);//类型转换
           lvo.add(tdv);
       }
        return lvo;
    }
}
